package day01;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.UUID;

public class Demo03 {
    public static void main(String[] args) {
        /**
         * LinkedList ArrayList 性能比较
         */
        LinkedList<String> list1 = new LinkedList<>();
        ArrayList<String> list2 = new ArrayList<>();
        for (int i=0; i<500000;i++){
            String uuid = UUID.randomUUID().toString();
            list1.add(uuid);
            list2.add(uuid);
        }
        //比较随机读取性能，从任意位置开始的读取性能
        // 头   1/4       中       3/4      尾
        // 0   125000   250000   375000   499999
        String s;
        long t1,t2,t3,t4,t5,t6;
        System.out.println("读取：");
        t1 = System.nanoTime();
        s = list1.get(0);
        t2 = System.nanoTime();
        s = list1.get(125000);
        t3 = System.nanoTime();
        s = list1.get(250000);
        t4 = System.nanoTime();
        s = list1.get(375000);
        t5 = System.nanoTime();
        s = list1.get(499999);
        t6 = System.nanoTime();
        System.out.println("LinkedList:");
        System.out.println("0:"+(t2-t1));
        System.out.println("125000:"+(t3-t2));
        System.out.println("250000:"+(t4-t3));
        System.out.println("375000:"+(t5-t4));
        System.out.println("499999:"+(t6-t5));

        t1 = System.nanoTime();
        s = list2.get(0);
        t2 = System.nanoTime();
        s = list2.get(125000);
        t3 = System.nanoTime();
        s = list2.get(250000);
        t4 = System.nanoTime();
        s = list2.get(375000);
        t5 = System.nanoTime();
        s = list2.get(499999);
        t6 = System.nanoTime();
        System.out.println("ArrayList:");
        System.out.println("0:"+(t2-t1));
        System.out.println("125000:"+(t3-t2));
        System.out.println("250000:"+(t4-t3));
        System.out.println("375000:"+(t5-t4));
        System.out.println("499999:"+(t6-t5));

        System.out.println("插入：");

        t1 = System.nanoTime();
        list1.add(0,"Demo07");
        t2 = System.nanoTime();
        list1.add(125000,"Demo07");
        t3 = System.nanoTime();
        list1.add(250000,"Demo07");
        t4 = System.nanoTime();
        list1.add(375000,"Demo07");
        t5 = System.nanoTime();
        list1.add(499999,"Demo07");
        t6 = System.nanoTime();
        System.out.println("LinkedList:");
        System.out.println("0:"+(t2-t1));
        System.out.println("125000:"+(t3-t2));
        System.out.println("250000:"+(t4-t3));
        System.out.println("375000:"+(t5-t4));
        System.out.println("499999:"+(t6-t5));

        t1 = System.nanoTime();
        list2.add(0,"Demo07");
        t2 = System.nanoTime();
        list2.add(125000,"Demo07");
        t3 = System.nanoTime();
        list2.add(250000,"Demo07");
        t4 = System.nanoTime();
        list2.add(375000,"Demo07");
        t5 = System.nanoTime();
        list2.add(499999,"Demo07");
        t6 = System.nanoTime();
        System.out.println("ArrayList:");
        System.out.println("0:"+(t2-t1));
        System.out.println("125000:"+(t3-t2));
        System.out.println("250000:"+(t4-t3));
        System.out.println("375000:"+(t5-t4));
        System.out.println("499999:"+(t6-t5));

        System.out.println("删除：");

        t1 = System.nanoTime();
        list1.remove(0);
        t2 = System.nanoTime();
        list1.remove(125000);
        t3 = System.nanoTime();
        list1.remove(250000);
        t4 = System.nanoTime();
        list1.remove(375000);
        t5 = System.nanoTime();
        list1.remove(499999);
        t6 = System.nanoTime();
        System.out.println("LinkedList:");
        System.out.println("0:"+(t2-t1));
        System.out.println("125000:"+(t3-t2));
        System.out.println("250000:"+(t4-t3));
        System.out.println("375000:"+(t5-t4));
        System.out.println("499999:"+(t6-t5));

        t1 = System.nanoTime();
        list2.remove(0);
        t2 = System.nanoTime();
        list2.remove(125000);
        t3 = System.nanoTime();
        list2.remove(250000);
        t4 = System.nanoTime();
        list2.remove(375000);
        t5 = System.nanoTime();
        list2.remove(499999);
        t6 = System.nanoTime();
        System.out.println("ArrayList:");
        System.out.println("0:"+(t2-t1));
        System.out.println("125000:"+(t3-t2));
        System.out.println("250000:"+(t4-t3));
        System.out.println("375000:"+(t5-t4));
        System.out.println("499999:"+(t6-t5));

    }
}
